package com.xiguthings.waka.web.controller.papercard;

import com.xiguthings.waka.dao.DataBaseStatus;
import com.xiguthings.waka.exception.BussinessException;
import com.xiguthings.waka.po.Paper;
import com.xiguthings.waka.po.PaperCard;
import com.xiguthings.waka.po.PaperCardExample;
import com.xiguthings.waka.po.PaperExample;
import com.xiguthings.waka.po.extpo.Page;
import com.xiguthings.waka.publicUtils.StringUtils;
import com.xiguthings.waka.service.paper.IPaperService;
import com.xiguthings.waka.service.papercard.IPaperCardService;
import com.xiguthings.waka.web.controller.base.AbstractController;
import com.xiguthings.waka.web.controller.base.ResponseEntity;
import com.xiguthings.waka.web.controller.growth.GrowthVo;
import com.xiguthings.waka.web.controller.paper.PaperVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/papercard")
@Api(tags = "奖励卡片")
public class PaperCardController extends AbstractController {
    @Resource
    private IPaperCardService paperCardService;
    @Resource
    private IPaperService paperService;

    /**
     * 创建查询条件
     *
     * @param paperCardVo
     * @param example
     * @throws NumberFormatException
     */
    private void queryConditions(PaperCardVo paperCardVo, PaperCardExample example) throws NumberFormatException {

        PaperCardExample.Criteria criteria = example.createCriteria();
        // 如果查询条件为空的
        if (paperCardVo == null) {
            // 则查询所有未被删除的
            criteria.andStatusNotEqualTo(DataBaseStatus.DELETE);
            return;
        }

        // 如果有查询条件
        PaperCard paperCard = paperCardVo.getPaperCard();
        // 添加查询条件
        if (paperCard != null) {

            if (StringUtils.isNotBlank(paperCard.getPaperCardId())) {
                criteria.andPaperCardIdEqualTo(paperCard.getPaperCardId());
            }
            if (StringUtils.isNotBlank(paperCard.getName())) {
                criteria.andNameLike("%" + paperCard.getName() + "%");
            }
            if (StringUtils.isNotBlank(paperCard.getDescr())) {
                criteria.andDescrLike(("%" + paperCard.getDescr() + "%"));
            }


        }

        if (StringUtils.isNotBlank(paperCardVo.getInStatus())) {
            // 添加包含的状态
            String statuses = paperCardVo.getInStatus();
            String[] statusArr = statuses.split(",");
            List<Integer> statusList = new ArrayList<>();
            for (String str : statusArr) {
                statusList.add(new Integer(str));
            }
            criteria.andStatusIn(statusList);
        } else {
            // 如果状态的查询条件是空的，则查询没有被删除的
            criteria.andStatusNotEqualTo(DataBaseStatus.DELETE);
        }


    }

    /**
     * 修改或者添加
     *
     * @param paperCardVo
     * @return
     * @throws Exception
     */
    @ApiOperation(tags = {
            "1.控制台"}, value = "修改/添加", httpMethod = "POST",
            notes = "修改时，必须填写“paperCard.paperCardId”参数。",
            response = ResponseEntity.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "paperCard.paperCardId", value = "主键id", dataType = "String", required = false, paramType = "query"),
            @ApiImplicitParam(name = "paperCard.name", value = "卡片名称", dataType = "String", required = false, paramType = "query"),
            @ApiImplicitParam(name = "paperCard.descr", value = "描述信息", dataType = "string", required = false, paramType = "query"),
            @ApiImplicitParam(name = "paperCard.status", value = "状态：0--正常，99--删除", dataType = "int", required = false, paramType = "query")})
    @RequestMapping("/save")
    @ResponseBody
    public ResponseEntity save(@ApiIgnore PaperCardVo paperCardVo) throws Exception {
        if (paperCardVo == null || paperCardVo.getPaperCard() == null) {
            BussinessException.throwThis(CODE_EMPTY_REQUIRED, "无法操作空数据");
        }

        PaperCard paperCard = paperCardVo.getPaperCard();
        String id = paperCard.getPaperCardId();
        String msg = null;
        if (StringUtils.isNotBlank(id)) {
            // 修改
            int row = paperCardService.updateByPrimaryKeySelective(paperCard);
            if (row > 0) {
                msg = "修改成功";
            } else {
                msg = "修改失败，可能数据库中不存在这条数据";
            }
        } else {
            // 添加
            int row = paperCardService.insert(paperCard);
            if (row > 0) {
                msg = "添加成功";
            } else {
                msg = "添加失败，请求重试";
            }

        }
        Paper paper = paperCardVo.getPaper();
        if (paper != null && StringUtils.isNotBlank(paper.getPaperId())) {
            Paper paperDB = new Paper();
            paperDB.setPaperId(paper.getPaperId());
            paperDB.setPaperCardId(paperCard.getPaperCardId());
            paperService.updateByPrimaryKeySelective(paperDB);
        }
        return ajaxReturnMap(CODE_SUCCESS, msg, paperCard.getPaperCardId());
    }

    @ApiOperation(tags = {"1.控制台",
            "2.用户端"}, value = "根据主键获取记录详情", httpMethod = "GET", notes = "paperCardId是必填", response = ResponseEntity.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "paperCard.paperCardId", value = "主键id", dataType = "String", required = true, paramType = "query")
    })
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseEntity detail(@ApiIgnore PaperCardVo paperCardVo) throws Exception {
        if (paperCardVo == null || paperCardVo.getPaperCard() == null) {
            BussinessException.throwThis(CODE_EMPTY_REQUIRED, "无法操作空数据");
        }

        String id = paperCardVo.getPaperCard().getPaperCardId();
        if (StringUtils.isBlank(id)) {
            BussinessException.throwThis(CODE_EMPTY_REQUIRED, "paperCardId是必填");
        }

        PaperCard paperCard = paperCardService.selectByPrimaryKey(id);
        return ajaxReturnMap(CODE_SUCCESS, null, paperCard);
    }

    @ApiOperation(tags = {"1.控制台",
            "2.用户端"}, value = "根据主键获取记录详情", httpMethod = "GET", notes = "paperCard.paperCardId是必填", response = ResponseEntity.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "paperCard.paperCardId", value = "主键id,如果是过个id，请用英文逗号分隔", dataType = "String", required = true, paramType = "query")})
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseEntity delete(@ApiIgnore PaperCardVo paperCardVo) throws Exception {
        if (paperCardVo == null || paperCardVo.getPaperCard() == null) {
            BussinessException.throwThis(CODE_EMPTY_REQUIRED, "无法操作空数据");
        }

        String id = paperCardVo.getPaperCard().getPaperCardId();
        if (StringUtils.isBlank(id)) {
            BussinessException.throwThis(CODE_EMPTY_REQUIRED, "paperCardId是必填");
        }

        int row = paperCardService.deleteByPrimaryKey(id);
        String msg = null;
        if (row > 0) {
            msg = "删除成功";
        } else {
            msg = "这条数据可能在此之前已经被删除了";
        }
        return ajaxReturnMap(CODE_SUCCESS, msg, null);
    }

    /**
     * 分页查询
     *
     * @param paperCardVo
     * @param page
     * @return
     * @throws Exception
     */

    @ApiOperation(tags = {"1.控制台",
            "2.用户端"}, value = "分页查询", httpMethod = "GET", notes = "可根据条件进行筛选", response = ResponseEntity.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "paperCard.paperCardId", value = "主键id", dataType = "String", required = false, paramType = "query"),
            @ApiImplicitParam(name = "paperCard.name", value = "卡片名称", dataType = "String", required = false, paramType = "query"),
            @ApiImplicitParam(name = "paperCard.descr", value = "描述信息", dataType = "string", required = false, paramType = "query"),
            @ApiImplicitParam(name = "inStatus", value = "状态：0--正常，99--删除", dataType = "int", required = false, paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页码，如果没有值，则使用默认值。开始值从 1 开始计算", dataType = "int", required = false, paramType = "query"),
            @ApiImplicitParam(name = "rows", value = "每页的数量,如果没有值，则使用默认值", dataType = "int", required = false, paramType = "query")})
    @RequestMapping("/pagelist")
    @ResponseBody
    public ResponseEntity pageList(@ApiIgnore PaperCardVo paperCardVo, @ApiIgnore Page page) throws Exception {
        PaperCardExample example = new PaperCardExample();
        queryConditions(paperCardVo, example);
        super.selectByPage(page, example, paperCardService);
        return ajaxReturnMap(CODE_SUCCESS, null, page);
    }

}
